// https://leetcode.cn/problems/island-perimeter/
// Created by ade on 2022/8/5.
//
#include <iostream>
#include <vector>

using namespace std;

class Solution {
public:
    int islandPerimeter(vector <vector<int>> &grid) {
        int row = grid.size();
        int col = grid[0].size();
        int sum = 0;
        for (int i = 0; i < row; i++) {
            for (int j = 0; j < col; j++) {
                if (grid[i][j] == 0) continue;
                sum += 4;
                if (i > 0 && grid[i - 1][j] == 1) sum--;
                if (i + 1 <= row - 1 && grid[i + 1][j] == 1) sum--;
                if (j > 0 && grid[i][j - 1] == 1) sum--;
                if (j < col - 1 && grid[i][j + 1] == 1) sum--;
            }
        }
        return sum;
    }
};

int main() {
    Solution so;
    vector <vector<int>> a = {
            {0, 1, 0, 0},
            {1, 1, 1, 0},
            {0, 1, 0, 0},
            {1, 1, 0, 0}
    };
    cout << so.islandPerimeter(a) << endl;
    return 0;
}